<?php
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
*/

/**
 * Description of TestManager
 *
 * @author rocco
 */
require_once 'TestDataManager.php';
require_once 'ClasseDataManager.php';
require_once 'TestStudentiDataManager.php';
require_once '../bean/Risultati.php';

define('FPDF_FONTPATH','../ufpdf/font/');
require('../ufpdf/ufpdf.php');

class TestManager
{

    private static $instance;

    public static function getInstance()
    {
        if(self::$instance == null)
        {
            $c = __CLASS__;
            self::$instance = new $c;
        }
        return self::$instance;
    }

    public function insertNewArgomento($connection, $codiceDisciplina, $nome, $descrizione)
    {
        TestDataManager::getInstance()->insertNewArgomento($connection, $codiceDisciplina, $nome, $descrizione);
    }

    public function insertQuestionVF($db, $tipologia, $codiceArgomento, $punteggio, $domanda, $rispCorretta, $link)
    {
        $codiceDomanda = TestDataManager::getInstance()->insertDomanda($db, $tipologia, $codiceArgomento, $punteggio, $domanda, $link);
        if($codiceDomanda != -1)
            TestDataManager::getInstance()->insertDettaglioRispostaVF($db, $codiceDomanda, $rispCorretta);

    }

    public function insertQuestionRM($db, $tipologia, $codiceArgomento, $punteggio, $domanda, $primaAlternativa,
              $secondaAlternativa, $terzaAlternativa, $quartaAlternativa, $alternativaCorretta, $link)
    {
        $codiceDomanda = TestDataManager::getInstance()->insertDomanda($db, $tipologia, $codiceArgomento, $punteggio, $domanda, $link);
        if($codiceDomanda != -1)
            TestDataManager::getInstance()->insertDettaglioRispostaRM($db, $codiceDomanda, $primaAlternativa, $secondaAlternativa, $terzaAlternativa, $quartaAlternativa, $alternativaCorretta);
    }

    public function insertTest($connection, $checks, $nomeTest, $oreTest, $minutiTest, $codiceDisciplina)
    {
        $codiceTest = TestDataManager::getInstance()->insertTest($connection, $nomeTest, $oreTest, $minutiTest, $codiceDisciplina);
        if($codiceTest != -1)
        {
            $domande = explode("*", $checks);
            for($i=0;$i<count($domande)-1;$i++)
                TestDataManager::getInstance()->insertTestDomanda($connection, $codiceTest, $domande[$i]);
        }
    }

    public function addQUestionToTest($connection, $checks, $codiceTest, $filtered)
    {
        $domande = explode("*", $checks);

        for($i=0;$i<count($domande)-1;$i++)
            if(!TestDataManager::getInstance()->isInTestDomanda($connection, $codiceTest, $domande[$i]))
                TestDataManager::getInstance()->insertTestDomanda($connection, $codiceTest, $domande[$i]);
    }


    public function sottomettiTest($connection, $codiceTest, $key, $classe)
    {
        $studenti = ClasseDataManager::getInstance()->getStudenti($connection, "", "", $classe);
        $domande = TestDataManager::getInstance()->searchDomandeByCodiceTest($connection, $codiceTest);

//        for($j=0;$j<count($domande); $j++)
//          echo ' '. $domande[$j]->getCodiceDomanda();

        for($i=0;$i<count($studenti); $i++)
        {
            //caldolo sequenza domande
            $seedQuestion = "";
            for($j=0;$j<count($domande); $j++)
            {
                $tmpD=array(count($domande));
                for($y=0;$y<count($domande);$y++)
                    $tmpD[$y]="";
            }
            for($j=0;$j<count($domande); $j++)
            {
                $flag=0;
                while($flag==0)
                {
                    srand((double)microtime()*100000000);
                    $indiceQ=rand(0, count($domande)-1);
//                    error_log("indiceQ " .$indiceQ );
                    if($tmpD[$indiceQ]=="")
                    {
//                        error_log("entrato ");
                        $tmpD[$indiceQ]="".$domande[$j]->getCodiceDomanda();
                        $flag=1;

//                        error_log("SItuazione attuale");
//                        for($n=0;$n<count($tmpD);$n++)
//                            error_log("$tmpD[$n]=" .$tmpD[$n]);
                    }
                }
            }
            for($j=0;$j<count($tmpD); $j++)
                if($seedQuestion == "")
                    $seedQuestion = $tmpD[$j];
                else
                    $seedQuestion = $seedQuestion."-".$tmpD[$j];

//            error_log("seedQ " . $seedQuestion);



            TestDataManager::getInstance()->sottomettiTest($connection, $codiceTest, $studenti[$i]->getCodiceStudente(), $key, $classe, $seedQuestion);
            for($j=0;$j<count($domande); $j++)
            {

                $tmpV=array(4);
                for($y=0;$y<4;$y++)
                    $tmpV[$y]="";

                if($domande[$j]->getTipologia()=='2')
                {
                    $cont=0;
                    $seedAlternative = '';
                    while($cont<4)
                    {
                        srand((double)microtime()*100000000);
                        $indice=rand(0, 3);
                        if($tmpV[$indice]=="")
                        {
                            $tmpV[$indice]='busy';
                            $tmp2=$indice+1;
                            if($seedAlternative=="")
                                $seedAlternative=$tmp2;
                            else
                                $seedAlternative=$seedAlternative . "-" . $tmp2;
                            $cont++;
                        }
                    }
                    TestDataManager::getInstance()->insertRisposteTest($connection, $codiceTest, $key, $studenti[$i]->getCodiceStudente(), $domande[$j]->getCodiceDomanda(), $seedAlternative);
                }
                else
                    TestDataManager::getInstance()->insertRisposteTest($connection, $codiceTest, $key, $studenti[$i]->getCodiceStudente(), $domande[$j]->getCodiceDomanda(), "");

            }

        }

    }


    public function calcolaPunteggioStudentiTest($connection, $codiceTest, $codiceStudente, $key)
    {
        $risultati = TestDataManager::getInstance()->getRisposteDate ($connection, $codiceStudente, $codiceTest, $key);
        $domande = TestDataManager::getInstance()->searchDomandeByCodiceTest($connection, $codiceTest);
        $punteggio=0;
        for($i=0;$i<count($risultati); $i++)
        {
            //echo 'risultati ' . $i . ' = '. $risultati[$i]->getCodiceDomanda() .' risp data ' . $risultati[$i]->getRispostaData();
            for($j=0;$j<count($domande); $j++)
                if($risultati[$i]->getCodiceDomanda()==$domande[$j]->getCodiceDomanda())
                {
                    //      echo ' entrato $domande[$j]->getCodiceDomanda() ' . $domande[$j]->getCodiceDomanda() . ' punt : ' .$domande[$j]->getPunteggio();
                    $rispostaData = $risultati[$i]->getRispostaData();
                    $rispostaCorretta = TestDataManager::getInstance()->getRispostaCorretta($connection, $domande[$j]->getCodiceDomanda(), $domande[$j]->getTipologia());
                    if($domande[$j]->getTipologia()=='1')
                    {
                        if($rispostaData == $rispostaCorretta)
                            $punteggio += $domande[$j]->getPunteggio();
                    }
                    else
                    {
                        $seedAlternative = TestDataManager::getInstance()->getSeedAlternative($connection, $codiceTest, $codiceStudente, $risultati[$i]->getCodiceDomanda());
                        $tmpSeed = explode("-", $seedAlternative);

                        $rispFromSeed=$tmpSeed[$risultati[$i]->getRispostaData()-1];

                        if($rispFromSeed == $rispostaCorretta)
                            $punteggio += $domande[$j]->getPunteggio();
                    }


                }
        }
        return $punteggio;
    }

    public function makePdf($connection, $codiceTest, $oreTest, $minutiTest, $key, $classe )
    {
        $studenti = ClasseDataManager::getInstance()->getStudenti($connection, '', '', $classe);
        $test = TestDataManager::getInstance()->getTestByCodice($connection, $codiceTest);
        $nomeTest = $test->getNome();

        $p = new UFPDF();
        $p->Open();
        $p->AddPage();
        $p->SetTextColor(0);
        $p->SetTitle("Partecipazioni test " . $nomeTest);
        $p->SetFont('Times', 'B', 12);
        $p->MultiCell(0,5, 'Nome Test : ' . $nomeTest . "\n".'Tempo a disposizione : ' . $oreTest . ' hh : ' . $minutiTest . ' mm' ."\n".'Key : ' . $key ."\n\n\n",0,'center');
        $p->SetFont('Arial', '', 12);
        $num = 1;
        for($i=0; $i<count($studenti); $i++)
        {
            $p->MultiCell(0,5, $num . ' Studente : ' .$studenti[$i]->getCognome() . ' ' .$studenti[$i]->getNome() . ' - Codice : '. $studenti[$i]->getCodiceStudente()."\n",0,'center');
            $num++;
        }
        $p->output();
    }

    public function makeTestPdf($connection, $codiceTest, $oreTest, $minutiTest, $key, $classe )
    {
        $test = TestDataManager::getInstance()->getTestByCodice($connection, $codiceTest);
        $studenti = ClasseDataManager::getInstance()->getStudenti($connection, '', '', $classe);
        $nomeTest = $test->getNome();

        $p = new UFPDF();
        $p->Open();
        $p->SetTitle("Test " . $nomeTest);

        for($ind=0;$ind<count($studenti);$ind++)
        {
            $p->AddPage();
            $p->SetTextColor(0);
            $studenteDetail = ClasseDataManager::getInstance()->getStudenteByCodice($connection, $studenti[$ind]->getCodiceStudente());
//            $listaDomande = TestStudentiDataManager::getInstance()->getListaDomandeStudente($connection, $studenti[$ind]->getCodiceStudente(), $codiceTest, $key);
            $listaDomandeTmp=TestStudentiDataManager::getInstance()->getSeedQuestion($connection, $studenti[$ind]->getCodiceStudente(), $codiceTest, $key);
            $listaDomande = explode("-", $listaDomandeTmp);
//            $p->MultiCell(0,5,  ''.count($listaDomandeTmp),0, 'center');
//            for($n=0;$n<count($listaDomande);$n++)
//               $p->MultiCell(0,5, "\n\n".'---- Domanda ' . $listaDomande[$n],0, 'center');
//            $arrayDomande = array(count($listaDomande));
            $p->SetFont('Arial', 'B', 12);
            $p->MultiCell(0,5, 'Nome Test : ' . $nomeTest . "\n".'Tempo a disposizione : ' . $oreTest . ' hh : ' . $minutiTest . ' mm' ."\n". 'Codice studente : ' .$studenti[$ind]->getCodiceStudente(). "\n" . 'Cognome e nome : ' .$studenti[$ind]->getCognome() . ' ' .$studenti[$ind]->getNome() ,0,'center');


            for($ind1=0;$ind1<count($listaDomande);$ind1++)
            {
                $indi = $ind1+1;
                $p->MultiCell(0,5, "\n\n".'Risposta Domanda ' . $indi . ' =  __',0, 'center');
            }
            $p->AddPage();

            for($i=0;$i<count($listaDomande); $i++)
            {
                $domanda = TestDataManager::getInstance()->getDomanda($connection, $listaDomande[$i]);
                $p->SetFont('Arial', 'B', 12);
                $indi=$i+1;
                $p->MultiCell(0,5, "\n\n".'Domanda ' . $indi ."\n\n");
                $p->SetFont('Arial', '', 12);
                $p->MultiCell(0,5,'Domanda : ' .$domanda->getTestoDomanda()."\n\n" ,0,'center');
                if($domanda->getLink() !="")
                {
                    list($width, $height, $type, $attr,$cinque, $sei, $sette) = getimagesize($domanda->getLink());

//                    $p->MultiCell(0,5,'width = ' . $width . ' hright : ' .$height. 'size ' . filesize($domanda->getLink()));
                    if($width>720 && $height >540)
                    {
                   //     $p->MultiCell(0,5,'resize');
                        $p->Image($domanda->getLink(), 20, $p->GetY()+5,100,100);
                        $p->SetY($p->GetY()+115);
                    }
                    else
                    {
                     //   $p->MultiCell(0,5,'original');
                        $p->Image($domanda->getLink(), 20, $p->GetY()+5);
                        $p->SetY($p->GetY()+($height-40));
                    }
                }
                if($domanda->getTipologia()=='1')
                {
                    $p->MultiCell(0,5,'V . Vero - F. Falso');
                }
                else
                {
                    $seedAlternative = TestDataManager::getInstance()->getSeedAlternative($connection, $codiceTest, $studenti[$ind]->getCodiceStudente(), $domanda->getCodiceDomanda());
                    $tmpSeed = explode("-", $seedAlternative);
                    $tmpSeedV = array(4);
                    for($u=0;$u<count($tmpSeed);$u++)
                    {
                        if($tmpSeed[$u]=='1')
                            $tmpSeedV[$u]=$domanda->getRisposta1();
                        if($tmpSeed[$u]=='2')
                            $tmpSeedV[$u]=$domanda->getRisposta2();
                        if($tmpSeed[$u]=='3')
                            $tmpSeedV[$u]=$domanda->getRisposta3();
                        if($tmpSeed[$u]=='4')
                            $tmpSeedV[$u]=$domanda->getRisposta4();
                    }


                    $p->MultiCell(0,5,'1. ' . $tmpSeedV[0] . "\n",0,'center');
                    $p->MultiCell(0,5,'2. ' . $tmpSeedV[1] . "\n",0,'center');
                    $p->MultiCell(0,5,'3. ' . $tmpSeedV[2] . "\n",0,'center');
                    $p->MultiCell(0,5,'4. ' . $tmpSeedV[3] . "\n",0,'center');
                }
            }
        }


        $p->output();
    }


//    public function makeTestPdf($connection, $codiceTest, $nomeTest, $oreTest, $minutiTest, $key, $classe )
//    {
//        $test = TestDataManager::getInstance()->getTestByCodice($connection, $codiceTest);
//        $studenti = ClasseDataManager::getInstance()->getStudenti($connection, '', '', $classe);
//
//        $p = new UFPDF();
//        $p->Open();
//        $p->SetTitle("Test " . $nomeTest);
//
//        for($ind=0;$ind<count($studenti);$ind++)
//        {
//            $p->AddPage();
//            $p->SetTextColor(0);
//            $studenteDetail = ClasseDataManager::getInstance()->getStudenteByCodice($connection, $studenti[$ind]->getCodiceStudente());
//            $listaDomande = TestStudentiDataManager::getInstance()->getListaDomandeStudente($connection, $studenti[$ind]->getCodiceStudente(), $codiceTest, $key);
//            $arrayDomande = array(count($listaDomande));
//            $p->SetFont('Arial', 'B', 12);
//            $p->MultiCell(0,5, 'Nome Test : ' . $nomeTest . "\n".'Tempo a disposizione : ' . $oreTest . ' hh : ' . $minutiTest . ' mm' ."\n". 'Codice studente : ' .$studenti[$ind]->getCodiceStudente(). "\n" . 'Cognome e nome : ' .$studenti[$ind]->getCognome() . ' ' .$studenti[$ind]->getNome() ,0,'center');
//
//
//            for($ind1=0;$ind1<count($listaDomande);$ind1++)
//            {
//                $p->MultiCell(0,5, "\n\n".'Risposta Domanda ' . $listaDomande[$ind1] . ' =  __',0, 'center');
//            }
//            $p->AddPage();
//            $arrayDomande = array();
//            for($ind1=0;$ind1<count($listaDomande);$ind1++)
//            {
////                echo ' inizializzo ' .$ind1;
//                $arrayDomande[$ind1]="";
//            }
//            for($i=0; $i<count($listaDomande); $i++)
//            {
//                $flag=TRUE;
//                while($flag==TRUE)
//                {
//                    srand((double)microtime()*100000000);
//                    $indice=rand(0,count($listaDomande)-1);
////                    echo 'indice ' . $indice . ' ';
//                    if($arrayDomande[$indice]=="")
//                    {
////                         $p->MultiCell(0,5, 'indice : ' . $indice, 0,'center');
//                        $arrayDomande[$indice]=$listaDomande[$i];
//
////                       echo $listeDomande[$i]->getCodiceDomanda(). '\n';
//                        $flag=FALSE;
//                    }
//                }
//            }
//            for($i=0;$i<count($arrayDomande); $i++)
//            {
//                $domanda = TestDataManager::getInstance()->getDomanda($connection, $arrayDomande[$i]);
//                $p->SetFont('Arial', 'B', 12);
//                $p->MultiCell(0,5, "\n\n".'Codice domanda ' . $domanda->getCodiceDomanda()."\n\n");
//                $p->SetFont('Arial', '', 12);
//                $p->MultiCell(0,5,'Domanda : ' .$domanda->getTestoDomanda()."\n\n" ,0,'center');
//
//                if($domanda->getTipologia()=='1')
//                {
//                    $p->MultiCell(0,5,'V . Vero - F. Falso');
//                }
//                else
//                {
//                    $seedAlternative = TestDataManager::getInstance()->getSeedAlternative($connection, $codiceTest, $studenti[$ind]->getCodiceStudente(), $domanda->getCodiceDomanda());
//                    $tmpSeed = explode("-", $seedAlternative);
//                    $tmpSeedV = array(4);
//                    for($u=0;$u<count($tmpSeed);$u++)
//                    {
//                        if($tmpSeed[$u]=='1')
//                            $tmpSeedV[$u]=$domanda->getRisposta1();
//                        if($tmpSeed[$u]=='2')
//                            $tmpSeedV[$u]=$domanda->getRisposta2();
//                        if($tmpSeed[$u]=='3')
//                            $tmpSeedV[$u]=$domanda->getRisposta3();
//                        if($tmpSeed[$u]=='4')
//                            $tmpSeedV[$u]=$domanda->getRisposta4();
//                    }
//
//
//                    $p->MultiCell(0,5,'1. ' . $tmpSeedV[0] . "\n",0,'center');
//                    $p->MultiCell(0,5,'2. ' . $tmpSeedV[1] . "\n",0,'center');
//                    $p->MultiCell(0,5,'3. ' . $tmpSeedV[2] . "\n",0,'center');
//                    $p->MultiCell(0,5,'4. ' . $tmpSeedV[3] . "\n",0,'center');
//                }
//            }
//        }
//
//
//        $p->output();
//    }
//


}
?>
